//
//  GBYEmtptyLoadingView.m
//  Swift_rog
//
//  Created by ayi on 2020/3/17.
//  Copyright © 2020 ayi. All rights reserved.
//

#import "GBYEmtptyLoadingView.h"

#import "UIView+LYExtension.h"
@interface GBYEmtptyLoadingView()

//@property (nonatomic, strong) UIActivityIndicatorView *loadingView;
@property (nonatomic, strong) UIView *loadingView;
@property (nonatomic, strong) UIImageView *animationImgv;
@property (nonatomic, strong) CABasicAnimation *animation;

@end

@implementation GBYEmtptyLoadingView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        NSBundle *bundle = [[NSBundle alloc]initWithPath:[[NSBundle mainBundle] pathForResource:@"MZEmpty" ofType:@"bundle"]];
        UIImage *img1 = [UIImage imageNamed:@"ico_refresh_circle" inBundle:bundle withConfiguration:nil];
        _animationImgv = [[UIImageView alloc] initWithImage:img1];
        
        [_loadingView addSubview:_animationImgv];
        
        _animationImgv.frame = CGRectMake(0, 0, 40, 40);
        _animationImgv.center = CGPointMake(_loadingView.frame.size.width/2, _loadingView.frame.size.height/2);

        _animation = [CABasicAnimation animationWithKeyPath:@"transform"];
        _animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
        _animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
        _animation.duration = 0.35;
        _animation.cumulative = YES;
        _animation.repeatCount = MAXFLOAT;
        _animation.removedOnCompletion = NO;
        
        
        [self addSubview:_loadingView];
        
        self.ly_size = CGSizeMake(50, 50);
        self.center = CGPointMake(50 * 0.5f, 50 * 0.5f);
        
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    
    UIView *view = self.superview;
    //不是UIView，不做操作
    if (view && [view isKindOfClass:[UIView class]]){
        self.ly_width = view.ly_width;
        self.ly_height = view.ly_height;
    }
    
    _loadingView.frame = CGRectMake(0, 0, 50, 50);
    _loadingView.center = CGPointMake(self.ly_width/2, self.ly_height/2);
    
    [self setupSubviews];
}

- (void)willMoveToSuperview:(UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];
    
    if (newSuperview && ![newSuperview isKindOfClass:[UIView class]]) return;
    if (newSuperview) {
        self.ly_width = newSuperview.ly_width;
        self.ly_height = newSuperview.ly_height;
    }
}

- (void)startAnimating{
//    [self.loadingView. startAnimating];
    [_animationImgv.layer addAnimation:_animation forKey:@"animationimgv"];
}
- (void)stopAnimating{
//    [self.loadingView stopAnimating];
    [_animationImgv.layer removeAllAnimations];
}

- (void)setupSubviews{
    if (self.contentViewY > 0) {
        self.loadingView.ly_y = self.contentViewY;
    }
}

@end
